package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.exception.BaseException;
import com.hmall.common.response.ItemUpdateStockVO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Date;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 修改商品
     * @param item
     */
    @Override
    public void update(Item item) {
        if(item.getStatus()==2){
        Date date = new Date();
        item.setUpdateTime(date);
        itemMapper.updateById(item);
        }
    }

    /**
     * 删除商品
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        Item item = itemMapper.selectById(id);
        if(item.getStatus()==2){
            itemMapper.deleteById(id);}
    }

    /**
     * 商品上下架
     * 上架1 下架2
     * @param id
     * @param status
     */
    @Override
    public void changeStatus(Long id, Integer status) {
        Item item = new Item();
        item.setStatus(status);
        item.setId(id);
        itemMapper.updateById(item);
        System.out.println("商品id为；"+id);
        //根据状态发送请求 到MQ队列，1 上架队列 2下架队列
        if(item.getStatus()==1){
            rabbitTemplate.convertAndSend(
                    MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,id);

       }else {
           rabbitTemplate.convertAndSend(
                   MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DELETE_KEY,id);

        }
    }

    /**
     * 扣减商品库存
     *
     * @param itemId 商品id
     * @param num    扣减数量
     */
    @Override
    public ItemUpdateStockVO updateStock(Long itemId, Integer num) {
        ItemUpdateStockVO result = new ItemUpdateStockVO();
        // 0-合法性校验
        if (Boolean.FALSE.equals(result.getUpdateResult())) {
            return result;
        }

        // 1-根据商品id查找现有库存
        Item item = itemMapper.selectById(itemId);
        if (Objects.isNull(item)) {
            result.setUpdateResult(Boolean.FALSE);
            result.setUpdateMsg("库存数据不存在，请稍后重试");
            return result;
        }

        // 2-数据合法性校验
        if (item.getStock() < num) {
            /*result.setUpdateResult(Boolean.FALSE);
            result.setUpdateMsg("库存不足，请稍后重试");
            return result;*/
            throw new BaseException("库存不足，请稍后重试");
        }

        // 3-更新
        Item updateItem = new Item();
        updateItem.setId(itemId);
        updateItem.setStock(item.getStock()-num);
        int count = itemMapper.updateById(updateItem);

        // 4-更新结果判断
        if (count==0){
            result.setUpdateResult(Boolean.FALSE);
            result.setUpdateMsg("更新失败，请稍后重试");
            return result;
        }
        result.setUpdateResult(Boolean.TRUE);
        result.setUpdateMsg("更新成功");
        return result;

    }

    /**
     * 恢复商品库存
     *
     * @param itemId 商品id
     * @param num    恢复数量
     */
    @Override
    public ItemUpdateStockVO addStock(Long itemId, Integer num) {
        ItemUpdateStockVO result = new ItemUpdateStockVO();
        // 0-合法性校验
        if (Boolean.FALSE.equals(result.getUpdateResult())) {
            return result;
        }

        // 1-根据商品id查找现有库存
        Item item = itemMapper.selectById(itemId);
        if (Objects.isNull(item)) {
            result.setUpdateResult(Boolean.FALSE);
            result.setUpdateMsg("库存数据不存在，请稍后重试");
            return result;
        }

        // 2-更新
        Item updateItem = new Item();
        updateItem.setId(itemId);
        updateItem.setStock(item.getStock()+num);
        int count = itemMapper.updateById(updateItem);

        // 4-更新结果判断
        if (count==0){
            result.setUpdateResult(Boolean.FALSE);
            result.setUpdateMsg("更新失败，请稍后重试");
            return result;
        }
        result.setUpdateResult(Boolean.TRUE);
        result.setUpdateMsg("更新成功");
        return result;
    }

}
